package com.saasitems.admin.web.controller;

import com.saasitems.admin.model.PartsType;
import com.saasitems.admin.service.ManagerUserService;
import com.saasitems.admin.service.PartsTypeService;
import com.saasitems.admin.web.vo.partstype.TreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2016/12/15.
 */
@Controller
public class PartsTypeController extends BaseController {

    @Autowired
    private PartsTypeService partsTypeService;

    @RequestMapping(value="admin/partstype/index",method= RequestMethod.GET)
    public ModelAndView index() {
        List<TreeVO> root = partsTypeService.getTree();
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("treeList", root);
        return new ModelAndView("admin/partstype/index", model);
    }

    @RequestMapping(value="admin/partstype/tree",method= RequestMethod.GET)
    public ModelAndView tree() {
        return new ModelAndView("admin/partstype/tree");
    }

    @RequestMapping(value="admin/partstype/add",method= RequestMethod.GET)
    public ModelAndView add() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("info", new PartsType());

        return new ModelAndView("admin/partstype/edit", model);
    }

    @RequestMapping(value="admin/partstype/add",method= RequestMethod.POST)
    @ResponseBody
    public Object add(PartsType vo, HttpServletRequest request) {

        if (vo != null) {
            vo.setEpId(100L);
            vo.setCreatetime(new Date());
            vo.setFlag(0);
            vo.setDepartmentId(0L);
            partsTypeService.insert(vo);
        }
        logger.info("更新成功");
        return this.renderSuccess("更新成功", this.convertUrl(request, "/admin/partstype/index.shtml"));
    }

    @RequestMapping(value="admin/partstype/edit/{id}/{fId}",method= RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id, @PathVariable("fId") Long fId) {
        Map<String, Object> model = new HashMap<String, Object>();

        if (fId != null && !fId.equals(0)) {
            PartsType pPt = partsTypeService.getInfo(fId);
            model.put("pInfo", pPt);
        }

        if (id != null && !id.equals(0)) {
            PartsType pt = partsTypeService.getInfo(id);
            model.put("info", pt);
        } else {
            model.put("info", new PartsType());
        }
        return new ModelAndView("admin/partstype/edit", model);
    }

    @RequestMapping(value="admin/partstype/edit",method= RequestMethod.POST)
    @ResponseBody
    public Object edit(PartsType vo, HttpServletRequest request) {
        if (vo.getId() != null && vo.getId() > 0) {
            int i = partsTypeService.update(vo);
            return this.renderSuccess("更新成功", this.convertUrl(request, "/admin/partstype/index.shtml"));
        } else {
            vo.setEpId(100L);
            vo.setCreatetime(new Date());
            vo.setFlag(0);
            vo.setDepartmentId(0L);
            partsTypeService.insert(vo);
            return this.renderSuccess("新增成功", this.convertUrl(request, "/admin/partstype/index.shtml"));
        }
    }
}
